import os
from xml.dom import minidom

import yaml

from Lib.zcm.assistant import FwVariables
from Lib.zcm.dcc.model import AVPItem


class Config(object):
    def __init__(self):
        self._config_path = FwVariables.get_value("DCC", "config_path", None)
        if not self._config_path:
            self._config_path = os.path.join(os.path.dirname(__file__), "config")

        # 加载字典
        file = self.get_config_file("dictDiameter.xml")
        doc = minidom.parse(file)
        self.dict_avps = doc.getElementsByTagName("avp")
        self.dict_vendors = doc.getElementsByTagName("vendor")
        self.dict_commands = doc.getElementsByTagName("command")
        # init dict vendors
        self.dict_vendors_map = {}
        self.dict_vendors_id_map = {}
        for vendor in self.dict_vendors:
            v_code = vendor.getAttribute("code")
            v_id = vendor.getAttribute("vendor-id")
            if v_id == 'None':
                v_id = ""
            self.dict_vendors_map[int(v_code)] = v_id
            self.dict_vendors_id_map[v_id] = int(v_code)
        # init avp map
        self.avp_dict_map = {}
        self.dict_avps_code_map = {}
        for avp in self.dict_avps:
            A = AVPItem()
            A.name = avp.getAttribute("name")
            A.code = avp.getAttribute("code")
            A.mandatory = avp.getAttribute("mandatory")
            A.type = avp.getAttribute("type")
            v_id = avp.getAttribute("vendor-id")
            self.dict_avps_code_map[",".join([A.code, v_id])] = A
            if v_id == "":
                A.vendor = 0
            else:
                A.vendor = self.dict_vendors_id_map[v_id]  #dictVENDORid2code(vId)
            if A.name:
                self.avp_dict_map[A.name] = A
        # init cmd map
        self.command_dict_map = {}
        self.command_dict_name_map = {}
        for command in self.dict_commands:
            cName = command.getAttribute("name")
            cCode = command.getAttribute("code")
            self.command_dict_map[int(cCode)] = cName
            self.command_dict_name_map[cName] = int(cCode)
        # init 数据类型
        self.asString = ["OctetString"]  # OctStr
        self.asUTF8 = ["UTF8String"]   # UTF8Str
        self.asI32 = ["Integer32"]   # INT32
        self.asU32 = ["Unsigned32"]  # Unsigned32
        self.asF32 = ["Float32"]   # UINT32
        self.asI64 = ["Integer64"]  # INT64
        self.asU64 = ["Unsigned64"]  # Unsigned64
        self.asF64 = ["Float64"]  # UINT64
        self.asIPAddress = ["IPAddress"]  # IPAddr
        self.asIP = ["IP"]
        self.asTime = ["Time"]  # Time
        dict_typedefs = doc.getElementsByTagName("typedef")
        for td in dict_typedefs:
            tName = td.getAttribute("name")
            tType = td.getAttribute("type")
            if tType in self.asString:
                self.asString.append(tName)
            if tType in self.asUTF8:
                self.asUTF8.append(tName)
            if tType in self.asU32:
                self.asU32.append(tName)
            if tType in self.asI32:
                self.asI32.append(tName)
            if tType in self.asI64:
                self.asI64.append(tName)
            if tType in self.asU64:
                self.asU64.append(tName)
            if tType in self.asF32:
                self.asF32.append(tName)
            if tType in self.asF64:
                self.asF64.append(tName)
            if tType in self.asIPAddress:
                self.asIPAddress.append(tName)
            if tType in self.asIP:
                self.asIP.append(tName)
            if tType in self.asTime:
                self.asTime.append(tName)

        self.cer_list = yaml.full_load(open(self.get_config_file("init_CER.yml")).read())
        self.dpr_list = yaml.full_load(open(self.get_config_file("init_DPR.yml")).read())
        self.dwa_list = yaml.full_load(open(self.get_config_file("init_DWA.yml")).read())
        self.dcc_config = yaml.load(open(self.get_config_file("init_CCR.yml"), encoding="utf-8").read(), Loader=yaml.FullLoader)
        self.env_config = yaml.full_load(open(self.get_config_file("env.yml"), encoding="utf-8").read())

    def get_config_file(self, *paths):
        return os.path.join(self._config_path, *paths)

    def get_cer_list(self):
        for i in self.cer_list:
            if i[0] == "Origin-Host":
                i[1] = FwVariables.get_origin_host()
            if i[0] == "Origin-Realm":
                i[1] = "chinatelecom.com"
        return self.cer_list

    def get_dpr_list(self):
        for i in self.dpr_list:
            if i[0] == "Origin-Host":
                i[1] = FwVariables.get_origin_host()
            if i[0] == "Origin-Realm":
                i[1] = "chinatelecom.com"
        return self.dpr_list

    def get_dwa_list(self):
        for i in self.dwa_list:
            if i[0] == "Origin-Host":
                i[1] = FwVariables.get_origin_host()
            if i[0] == "Origin-Realm":
                i[1] = "chinatelecom.com"
        return self.dwa_list

    @property
    def prefix(self):
        # 本国国家码
        return "60"

    @property
    def DccCfg(self):
        return self.dcc_config

    @property
    def env(self):
        return self.env_config

dcc_cfg = Config()